module.exports = app => {
	const { Sequelize } = app;
	const task = app.model.define(
		'tasks',
		{
			id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
			name: Sequelize.TEXT,
			project_id: Sequelize.INTEGER(10),
			parent_id: Sequelize.INTEGER(11),
			creator_id: Sequelize.INTEGER(10),
			task_list_id: Sequelize.INTEGER(11),
			task_state_id: Sequelize.INTEGER(11),
			task_type_id: Sequelize.INTEGER(11),
			task_priority_id: Sequelize.INTEGER(11),
			executor_id: Sequelize.INTEGER(11),
			start_date: Sequelize.STRING(30),
			end_date: Sequelize.STRING(30),
			remark: Sequelize.TEXT,
			is_done: Sequelize.TINYINT(1),
			is_privacy: Sequelize.TINYINT(1),
			is_recycle: Sequelize.TINYINT(1),
			likes: Sequelize.INTEGER(11),
			plan_work_hours: Sequelize.FLOAT(10, 2),
			sort: Sequelize.DOUBLE(32, 16),
		},
		{}
	);
	task.associate = function(models) {
		// 主要执行者 - 一对一关联（通过 executor_id 字段）
		task.hasOne(app.model.Users, { foreignKey: 'id', sourceKey: 'executor_id', as: 'executor' });
		
		// 创建者 - 一对一关联（通过 creator_id 字段）
		task.hasOne(app.model.Users, { foreignKey: 'id', sourceKey: 'creator_id', as: 'creator' });
		
		// 项目 - 一对一关联（通过 project_id 字段）
		task.hasOne(app.model.Projects, { foreignKey: 'id', sourceKey: 'project_id', as: 'project' });
		
		// 参与者 - 多对多关联（通过 user_tasks 中间表，包括主要执行者和其他协作者）
		app.model.Tasks.belongsToMany(app.model.Users, {
			through: app.model.UserTasks,
			foreignKey: 'task_id',
			otherKey: 'user_id',
			as: 'participators',
		});
		
		// 点赞者 - 多对多关联（通过 user_task_likes 中间表）
		// app.model.Tasks.belongsToMany(app.model.Users, {
		// 	through: app.model.UserTaskLikes,
		// 	foreignKey: 'task_id',
		// 	otherKey: 'user_id',
		// 	as: 'likers',
		// });
	};
	return task;	
}